package Single;

public class test {
    public static void main(String[] args) {
        // 创建多个线程来测试单例模式的线程安全性
        for (int i = 0; i < 5; i++) {
            BankRunnable bankRunnable = new BankRunnable();
            Thread thread = new Thread(bankRunnable);
            thread.setName("Thread-" + i);
            thread.start();
        }
    }
}

class Bank {
    private Bank() {
    }

    private static volatile Bank instance = null;

    public static Bank getInstance() {
        if (instance == null) {
            synchronized (Bank.class) {
                if (instance == null)
                    instance = new Bank();
            }
        }
        return instance;
    }
}

class BankRunnable implements Runnable {

    public void run() {
        for (int i = 0; i < 100; i++) {
            Bank bank = Bank.getInstance();
            System.out.println(Thread.currentThread().getName() + ", Instance: " + bank);
        }
    }
}
